/*
 * @(#) $Id: StringUtil.java,v 1.10 2012/09/26 05:06:11 soonwoo Exp $
 * 
 * CafeShow Service
 * 
 * Copyright 2012 eZENsolution Co., Ltd. All rights reserved.
 * 601, Daerung Post Tower II, 182-13, Guro 3-dong, Guro-gu
 * Seoul, Korea
*/
/**
 * 
 */
package com.kissoft.common.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;

/**
 * TODO Insert type comment for StringUtil.
 *
 * @author guidda
 * @version $Revision: 1.10 $
 */
public class StringUtil {
	public static String subStringToken(String str, String backStr, int len){
		if(str == null || str.length() == 0){
			return "";
		}
		
		if(str.length() <= len){
			return str;
		}
		
		str = str.substring(0, len) + backStr;
		
		return str;
	}
	
	public static String nextLineStringToken(String str, int len){
		if(str == null || str.length() == 0){
			return "";
		}
		
		if(str.length() <= len){
			return str;
		}
		
		String temp_str = "";
		int loop_cnt = str.length() / len;
		
		for(int i=0; i<loop_cnt; i++){
			if(i == 0){
				temp_str += str.substring(0, len) + "<br/>";
			}else{
				temp_str += str.substring(len*i, len+(len*i)) + "<br/>";
			}
		}
		
		System.out.println("temp_str  :" + temp_str);
		
		return temp_str;
	}
	
	/**
	 * *일전, *시간전 ... 으로 표현
	 * @param date
	 * @return
	 */
	public static String getDiff(Date date) {
		Date todayDate = new Date();
		Date postDate = date;

		Calendar todayCalendar = Calendar.getInstance();
		Calendar postCalendar = Calendar.getInstance();
		/*
		System.out.println("reg_date : " + DateFormatUtils.format(date, "yyyy-MM-dd HH:mm:ss"));
		Date temp_date = new Date();
		temp_date = todayCalendar.getTime();
		System.out.println("now_date : " + DateFormatUtils.format(temp_date, "yyyy-MM-dd HH:mm:ss"));
		*/
		todayCalendar.setTime(todayDate);
		postCalendar.setTime(postDate);

		long milliseconds1 = todayCalendar.getTimeInMillis();
		long milliseconds2 = postCalendar.getTimeInMillis();
		long diff = milliseconds2 - milliseconds1;
		long diffSeconds = diff / 1000;
		long diffMinutes = diff / (60 * 1000);
		long diffHours = diff / (60 * 60 * 1000);
		long diffDays = diff / (24 * 60 * 60 * 1000);
		long diffResult = 0;
		String diffType = "";

		if (diffDays < 0) {
			//diffResult = diffDays;
			//diffType = "일 전";
			
			return DateFormatUtils.format(date, "yyyy.MM.dd HH:mm");
		} else if (diffHours < 0) {
			diffResult = diffHours;
			diffType = "시간 전";
		} else if (diffMinutes < 0) {
			diffResult = diffMinutes;
			diffType = "분 전";
		} else if (diffSeconds < 0) {
			diffResult = diffSeconds;
			diffType = "초 전";
		}
		
		if(diffResult == 0){
			return "지금 막";
		}
		diffResult = Math.abs(diffResult);
		SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
		return String.valueOf(diffResult)
				+ diffType;
	}
	
	public static String replacements(String str){
		if (StringUtils.isEmpty(str)) return "";
		
		return str.replaceAll("(\r\n|\n\r|\r|\n)", "<br />");
	}
	
	public static String replacementsEnter(String str){
		if (StringUtils.isEmpty(str)) return "";
		
		return str.replaceAll("(\r\n|\n\r|\r|\n)", " ");
	}
	
	public static String contentTagReplace(String str){
		if(str == null){
			return "";
		}
		
		if("".equals(str)){
			return "";
		}
		
		str = str.replaceAll("<script>", "&lt;script&gt;");
		str = str.replaceAll("</script>", "&lt;/script&gt;");

		return str;
	}
	
	public static String contentTagReplaceAll(String contents){
		if(contents == null){
			return "";
		}
		
		if("".equals(contents)){
			return "";
		}
		contents = contents.replaceAll("&lt;", "＜");
		contents = contents.replaceAll("&gt;", "＞");
		contents = contents.replaceAll("<script", "＜script");
		contents = contents.replaceAll("</script", "＜/script");
		contents = contents.replaceAll("<object", "＜object");
		contents = contents.replaceAll("</object", "＜/object");
		contents = contents.replaceAll("<iframe", "＜iframe");
		contents = contents.replaceAll("</iframe", "＜/iframe");
		
		//2012-10-11 html 태그 제거 새로추가
		Pattern JUSUK = Pattern.compile("<!--.*?-->",Pattern.DOTALL);  
	    Pattern SCRIPTS = Pattern.compile("<(no)?script[^>]*>.*?</(no)?script>",Pattern.DOTALL);  
	    Pattern STYLE = Pattern.compile("<style[^>]*>.*</style>",Pattern.DOTALL);  
	    Pattern OBJECTS = Pattern.compile("<object[^>]*>.*</object>",Pattern.DOTALL);  
	    Pattern OBJECT = Pattern.compile("<object[^>]*/>",Pattern.DOTALL);  
	    Pattern IFRAMES = Pattern.compile("<iframe[^>]*>.*</iframe>",Pattern.DOTALL);  
	    Pattern IFRAME = Pattern.compile("<iframe[^>]*/>",Pattern.DOTALL);  
	      
	    Matcher m;  
	    
	    m = JUSUK.matcher(contents);  
	    contents = m.replaceAll("");    
	    m = SCRIPTS.matcher(contents);  
	    contents = m.replaceAll("");  
	    m = STYLE.matcher(contents);  
	    contents = m.replaceAll("");  
	    m = OBJECTS.matcher(contents);  
	    contents = m.replaceAll("");  
	    m = OBJECT.matcher(contents);  
	    contents = m.replaceAll("");  
	    m = IFRAMES.matcher(contents);  
	    contents = m.replaceAll("");
	    m = IFRAME.matcher(contents);  
	    contents = m.replaceAll("");
		
		return contents;
	}
	
	/*	
	public static void main(String[] args){
		Calendar calendar = Calendar.getInstance();

		//calendar.add(Calendar.DAY_OF_MONTH, -1);
		
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		
		//calendar.add(Calendar.HOUR, -23);
		//calendar.add(Calendar.MINUTE, -59);
		
		Date date = new Date();
		date = calendar.getTime();
		
		System.out.println(DateFormatUtils.format(date, "yyyy-MM-dd HH:mm:ss"));
		
		System.out.println(getDiff(date));
	}
	*/
}
